package com.ts.main.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.admin.context.UserContext;
import com.ts.main.admin.dto.AdminRoleDto;
import com.ts.main.admin.dto.AdminRoleSearchPageDto;
import com.ts.main.admin.entity.AdminRoleEntity;
import com.ts.main.admin.exception.RPage;
import com.ts.main.admin.mapper.AdminRoleMapper;
import com.ts.main.admin.vo.AdminRoleVo;
import com.ts.main.admin.vo.AdminRolePartVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lk
 * @description 针对表【t_admin_role(角色表)】的数据库操作Service实现
 * @createDate 2025-06-09 16:48:27
 */
@AllArgsConstructor
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRoleEntity> {

    public RPage<List<AdminRoleVo>> getRolePage(AdminRoleSearchPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<AdminRoleVo> list = baseMapper.getAdminRolePage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public AdminRoleVo getRoleDetails(Integer roleId) {
        return BeanUtil.copyProperties(getById(roleId), AdminRoleVo.class);
    }

    public Boolean saveRole(AdminRoleDto dto) {
        dto.setUserId(UserContext.getUser().getId());
        return save(BeanUtil.copyProperties(dto, AdminRoleEntity.class));
    }

    public Boolean updateRole(AdminRoleDto dto) {
        dto.setUserId(UserContext.getUser().getId());
        return updateById(BeanUtil.copyProperties(dto, AdminRoleEntity.class));
    }

    public Boolean deleteRole(Integer roleId) {
        LambdaUpdateWrapper<AdminRoleEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper
                .set(AdminRoleEntity::getIsDeleted, true)
                .eq(AdminRoleEntity::getId, roleId);
        return update(wrapper);
    }

    public List<AdminRolePartVo> getRoleList() {
        LambdaQueryWrapper<AdminRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleEntity::getIsDeleted, false);
        return BeanUtil.copyToList(baseMapper.selectList(queryWrapper), AdminRolePartVo.class);
    }
}




